package org.erepublik.company.controllers;

import java.util.ArrayList;
import java.util.Arrays;

import org.erepublik.company.EProductType;
import org.erepublik.company.log.SitesLogger;
import org.erepublik.company.product.Weapons;
import org.erepublik.company.searchCriteria.EAllianceSearchCriteria;
import org.erepublik.company.searchCriteria.EQualityFilter;
import org.erepublik.company.service.IProductsService;
import org.erepublik.company.service.WeaponSearchCriteria;
import org.erepublik.company.utils.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/weapons.html")
public class WeaponsController extends AbstractController  {

    @Autowired // autumatycznie wstrzykiwany obiekt
    private SitesLogger sitesLogger; 

    @Autowired // autumatycznie wstrzykiwany obiekt
    private IProductsService productService; 
    
    @RequestMapping(method = RequestMethod.GET)
    public String showForm(ModelMap model) {
        fillModelMap(model);
        WeaponSearchCriteria criteria = new WeaponSearchCriteria();
        Weapons[] products = productService.getWeapons(null, criteria.getMinimalDesign(), criteria.getWeaponType().getProductType() );
        products = qualityFilter(products, EQualityFilter.Q5);
        products = shrinkTable(products);
        model.addAttribute("updateTime", productService.getMinutesSinceWeaponUpdate(EProductType.TANK) );
        model.addAttribute("products", products);
        model.addAttribute("weaponsSearchCriteria",criteria);

        return "weapons";
    }

    @RequestMapping(method = RequestMethod.POST)
    public String showList(ModelMap model, @ModelAttribute("weaponsSearchCriteria") WeaponSearchCriteria criteria) {
        fillModelMap(model);
        Weapons[] products = productService.getWeapons(criteria.getCountry(), 
                    criteria.getMinimalDesign(), criteria.getWeaponType().getProductType() );
        Arrays.sort(products, criteria.getComparator().getComparator());
        if (criteria.getQualityFilter()!= EQualityFilter.ALL)
            products = qualityFilter(products, criteria.getQualityFilter());
        products = filterAlliances(criteria.getAlliance(), products);
        products = shrinkTable(products);
        model.addAttribute("updateTime", productService.getMinutesSinceWeaponUpdate( 
                EProductType.valueOf( criteria.getWeaponType().toString() )));
        model.addAttribute("products", products);

        return "weapons";
    }
    
    
    private Weapons[] qualityFilter(Weapons[] products, EQualityFilter filter) {
        ArrayList<Weapons> weapons = new ArrayList<Weapons>();
        for (Weapons house : products) {
            if (house.getQuality() == filter.getQuality())
                weapons.add(house);
        }
        return weapons.toArray(new Weapons[0]);
    }
    
    
    final int MAX_SIZE=50; // maksymalna ilosc wyswietlanych produktow
    // skraca tabele do MAX_SIZE produktow
    private Weapons[] shrinkTable(Weapons[] products) {
        if (products.length <= MAX_SIZE)
            return products;
        else {
            Weapons[] newTable = new Weapons[MAX_SIZE];
            for (int i=0; i < MAX_SIZE; i++) {
                newTable[i]=products[i];
            }
            return newTable;
        }
    }
    
    
    private Weapons[] filterAlliances(EAllianceSearchCriteria alliance, Weapons[] products) {

        ArrayList<Weapons> list = new ArrayList<Weapons>();
        for (Weapons food : products) {
            if ( Utils.contains( food.getCountry().getAlliance(), alliance.getAlliances() ) )
                list.add(food);
        }
        return list.toArray(new Weapons[0]);
    }
    
    
}
